package com.fintech.third.edczxutil.style;

import java.io.File;
import java.io.InputStream;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.fintech.third.edczxutil.handle.TypeHandle;
import com.fintech.third.utils.ThirdSystemUtil;

public class InitStyle {
	public static void main(String[] args) throws Exception{
		init();
	}
	
	public static void init() throws Exception{
		//先解析style
		Style style=new Style();
		SAXReader saxReader = new SAXReader();
		try {
			//ClassLoader classLoader = UtilXmlBean.class.getClassLoader();
			//String url = Thread.currentThread().getContextClassLoader().getResource("/configuration/personalBaoGao.xml").getPath();  
			//InputStream resourceAsStream = SystemUtil.getClass().getResourceAsStream("/resources/personalBaoGao.xml");
			//URL url= InitStyle.class.getResource("../util/personalBaoGao.xml");
			//URL url= classLoader.getResource("personalBaoGao.xml"); //TODO 循环解析所有的接口映射文件
			InputStream in = ThirdSystemUtil.class.getResourceAsStream("/configuration/personalBaoGao.xml");
			Document douc= saxReader.read(in);
			Element root= douc.getRootElement();
			Element title = root.element("title");
			style.setId(title.attributeValue("id"));
			style.setName(title.attributeValue("name"));
			style.setBean(title.attributeValue("bean"));
			List<Element> elements =root.elements("element");
			for (Element element : elements) {
				StyleElement elem=new StyleElement();
				elem.setTitle(element.attributeValue("title"));
				elem.setType(element.attributeValue("type"));
				elem.setValue(element.attributeValue("value"));
				elem.setBean(element.attributeValue("bean"));
				if(element.element("element")!=null){
					pareElement(element, elem);
				}
				if(element.element("field")!=null){
					pareField(element, elem);
				}
				style.addEleament(elem);
			}
			StyleContext.addStyle(style);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 递归解析Element
	 * @param element
	 * @param styleElement
	 * @throws Exception 
	 */
	public static void pareElement(Element element,StyleElement styleElement) throws Exception{
		List<Element> elements = element.elements("element");
		for (Element ele : elements) {
			StyleElement elem = new StyleElement();
			elem.setTitle(ele.attributeValue("title"));
			elem.setType(ele.attributeValue("type"));
			elem.setValue(ele.attributeValue("value"));
			elem.setBean(ele.attributeValue("bean"));
			if(ele.element("element")!=null){
				pareElement(ele, elem);
			}
			if(ele.element("field")!=null){
				pareField(ele, elem);
			}
			styleElement.addElement(elem);
		}
	}
	
	/**
	 * 解析Field
	 * @param element
	 * @param elem
	 * @throws ClassNotFoundException 
	 * @throws IllegalAccessException 
	 * @throws Exception 
	 */
	public static void pareField(Element element,StyleElement elem) throws Exception{
		List<Element> fields = element.elements("field");
		for (Element field : fields) {
			StyleField fi=new StyleField();
			fi.setType(field.attributeValue("type"));
			fi.setValue(field.attributeValue("value"));
			fi.setParent(field.attributeValue("parent"));
			fi.setBean(field.attributeValue("bean"));
			fi.setName(field.getText());
			if(null!=field.attributeValue("typeHandle")){
				fi.setTypeHandle((TypeHandle)Class.forName(field.attributeValue("typeHandle")).newInstance());
			}
			elem.addField(fi);
		}
	}
	
}
